package main.activities;

import java.util.Calendar;

import com.google.gson.Gson;

import main.alarms.AlarmInfo;
import main.alarms.AlarmsManager;
import main.alarms.MainActivity;
import main.alarms.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class NewAlarmActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_alarms);
		//AlarmsManager am = new AlarmsManager(new MainActivity());
		final EditText addNewAlarmDescriptionText=(EditText)this.findViewById(R.id.addNewAlarmDescriptionText);
		Button addNewAlarmBtnCancel=(Button)this.findViewById(R.id.addNewAlarmBtnCancel);
		Button addNewAlarmBtnOk=(Button)this.findViewById(R.id.addNewAlarmBtnOk);
		final TimePicker AddNewAlarmTimePicker=(TimePicker)this.findViewById(R.id.AddNewAlarmTimePicker);
		
		// 取消操作
		addNewAlarmBtnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		// 点击OK添加或者编辑闹钟
		addNewAlarmBtnOk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(NewAlarmActivity.this, "OK", Toast.LENGTH_LONG);
				
				AlarmInfo info=new AlarmInfo();
				
				Calendar c=Calendar.getInstance();
				c.setTimeInMillis(System.currentTimeMillis());
				c.set(Calendar.HOUR_OF_DAY, AddNewAlarmTimePicker.getCurrentHour());
				c.set(Calendar.MINUTE, AddNewAlarmTimePicker.getCurrentMinute());
				c.set(Calendar.SECOND, 0);
				c.set(Calendar.MILLISECOND,0);
				info.time=c;
				info.descriptin=addNewAlarmDescriptionText.getText().toString();
				Gson gson = new Gson();
				String info_json = gson.toJson(info);
				Intent i=new Intent();
				setResult(Activity.RESULT_OK, i);  
				i.putExtra("info_json", info_json); //直接使用json一次传递数据
				
				//startActivity(i);
				
				finish();
			}
		});
	}
}
